import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { rules } from '/@/utils/helper/validator';
import { render } from '/@/utils/common/renderUtils';
import { getWeekMonthQuarterYear } from '/@/utils';
//列表数据
export const columns: BasicColumn[] = [
  {
    title: '设备一级分类',
    align: 'center',
    dataIndex: 'deviceMark',
    customRender: ({ text }) => {
      // 使用同步渲染方法
      return text ? render.renderDict(text, 'device_mark') : '--';
    },
  },
  {
    title: '设备二级分类',
    align: 'center',
    dataIndex: 'deviceCla',
  },
  {
    title: '设备三级分类',
    align: 'center',
    dataIndex: 'deviceCategory',
  },
  {
    title: '需要处理字段',
    align: 'center',
    dataIndex: 'needField',
  },
  {
    title: '告警规则',
    align: 'center',
    dataIndex: 'alarmRule',
  },
  {
    title: '最大值',
    align: 'center',
    dataIndex: 'max',
  },
  {
    title: '最小值',
    align: 'center',
    dataIndex: 'min',
  },
  {
    title: '数值',
    align: 'center',
    dataIndex: 'numValue',
  },
];
//查询数据
export const searchFormSchema: FormSchema[] = [
  {
    label: '设备一级分类',
    field: 'deviceMark',
    component: 'Input',
    //colProps: {span: 6},
  },
  {
    label: '设备二级分类',
    field: 'deviceCla',
    component: 'Input',
    //colProps: {span: 6},
  },
  {
    label: '设备三级分类',
    field: 'deviceCategory',
    component: 'Input',
    //colProps: {span: 6},
  },
  {
    label: '需要处理字段',
    field: 'needField',
    component: 'Input',
    //colProps: {span: 6},
  },
];
//表单数据
export const formSchema: FormSchema[] = [
  {
    label: '设备一级分类',
    field: 'deviceMark',
    component: 'Input',
  },
  {
    label: '设备二级分类',
    field: 'deviceCla',
    component: 'Input',
  },
  {
    label: '设备三级分类',
    field: 'deviceCategory',
    component: 'Input',
  },
  {
    label: '需要处理字段',
    field: 'needField',
    component: 'Input',
  },
  {
    field: 'alarmType',
    label: '告警类型',
    component: 'JDictSelectTag',
    componentProps: () => {
      return {
        dictCode: 'alarm_type',
      };
    },
  },
  {
    label: '告警规则',
    field: 'alarmRule',
    component: 'Select',
    componentProps: {
      options: [
        { label: '区间', value: '区间' },
        { label: '数值', value: '数值' },
      ],
      // 添加值变化时的回调
      onChange: (value) => {
        // 这里实际使用时需要获取表单实例，但配置中通常通过动态属性处理
      },
    },
  },
  {
    label: '最大值',
    field: 'max',
    component: 'InputNumber',
    show: ({ values }) => values?.alarmRule === '区间',
    dynamicRules: ({ model }) => {
      return [
        {
          validator: (_rule, value) => {
            if (value != null && model.min != null && value < model.min) {
              return Promise.reject('最大值不能小于最小值');
            }
            return Promise.resolve();
          },
          trigger: 'blur',
        },
      ];
    },
  },
  {
    label: '最小值',
    field: 'min',
    component: 'InputNumber',
    show: ({ values }) => values?.alarmRule === '区间',
    dynamicRules: ({ model }) => {
      return [
        {
          validator: (_rule, value) => {
            if (value != null && model.max != null && value > model.max) {
              return Promise.reject('最小值不能大于最大值');
            }
            return Promise.resolve();
          },
          trigger: 'blur',
        },
      ];
    },
  },
  {
    label: '数值',
    field: 'numValue',
    component: 'Input',
    show: ({ values }) => values?.alarmRule === '数值',
  },
  // TODO 主键隐藏字段，目前写死为ID
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    label: '',
    field: 'agreementId',
    component: 'Input',
    show: false,
  },
  {
    label: '',
    field: 'deviceId',
    component: 'Input',
    show: false,
  },
];

// 高级查询数据
export const superQuerySchema = {
  deviceMark: { title: '设备一级分类', order: 0, view: 'text', type: 'string' },
  deviceCla: { title: '设备二级分类', order: 1, view: 'text', type: 'string' },
  deviceCategory: { title: '设备三级分类', order: 2, view: 'text', type: 'string' },
  needField: { title: '需要处理字段', order: 3, view: 'text', type: 'string' },
  alarmRule: { title: '告警规则', order: 4, view: 'text', type: 'string' },
  max: { title: '最大值', order: 5, view: 'number', type: 'number' },
  min: { title: '最小值', order: 6, view: 'number', type: 'number' },
  numValue: { title: '数值', order: 7, view: 'number', type: 'number' },
};

/**
 * 流程表单调用这个方法获取formSchema
 * @param param
 */
export function getBpmFormSchema(_formData): FormSchema[] {
  // 默认和原始表单保持一致 如果流程中配置了权限数据，这里需要单独处理formSchema
  return formSchema;
}
